﻿Imports FinanceiroDAL
Imports System.Text.RegularExpressions
Public Class ContasPagarBLL
    Dim objDAL As ContasPagarDAL
#Region "PROPRIEDADES"
    Property ctp_codigo As Integer
    Property ccc_codigo As Nullable(Of Integer)
    Property com_codigo As Nullable(Of Integer)
    Property ctp_dtcompra As Date
    Property ctp_descricao As String
    Property ctp_fornprestador As String
    Property lCtasPagar As New List(Of ItensCtasPagarBLL)
#End Region
#Region "CONSTRUTORES"
    Sub New()
    End Sub
    Sub New(ByVal ctpcodigo As Integer, ByVal cccodigo As Nullable(Of Integer), ByVal comcodigo As Nullable(Of Integer), ByVal ctpdtcompra As Date, ByVal ctpobs As String, ByVal forprest As String)
        Me.ctp_codigo = ctpcodigo
        Me.ccc_codigo = cccodigo
        Me.com_codigo = comcodigo
        Me.ctp_dtcompra = ctpdtcompra
        Me.ctp_descricao = ctpobs
        Me.ctp_fornprestador = forprest
    End Sub
    Sub New(codigo)
        carrega(codigo)
    End Sub
#End Region
#Region "limpar"
    Sub limpar()
        Me.ctp_codigo = Nothing
        Me.ccc_codigo = Nothing
        Me.com_codigo = Nothing
        Me.ctp_dtcompra = Nothing
        Me.ctp_descricao = Nothing
        Me.ctp_fornprestador = Nothing
        lCtasPagar.Clear()
    End Sub
#End Region
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ContasPagarDAL
            tab = objDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.ctp_codigo = Integer.Parse(tab.Rows(0)("ctp_codigo").ToString)
                If (tab.Rows(0)("ccc_codigo") Is DBNull.Value) Then
                    Me.com_codigo = Nothing
                Else
                    Me.ccc_codigo = tab.Rows(0)("ccc_codigo").ToString
                End If
                If (tab.Rows(0)("com_codigo") Is DBNull.Value) Then
                    Me.com_codigo = Nothing
                Else
                    Me.com_codigo = tab.Rows(0)("com_codigo").ToString
                End If
                Me.ctp_dtcompra = tab.Rows(0)("ctp_dtcompra").ToString
                Me.ctp_descricao = tab.Rows(0)("ctp_descricao").ToString
                Me.ctp_fornprestador = tab.Rows(0)("ctp_fornprestador").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Buscar CBFiltro"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New ContasPagarDAL
            ret = objDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Buscar Pessoa"
    Function buscarP(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New ContasPagarDAL
            ret = objDAL.buscarP(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Localiza dAta"
    Function locdata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            objDAL = New ContasPagarDAL
            If dataIni <= datafim Then
                ret = objDAL.locadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Buscar ItensCtasPagar"
    Public Function buscaritens() As DataTable
        Dim tab As New DataTable
        Try
            Dim objitens As New ItensCtasPagarBLL
            tab = objitens.localizar(Me.ctp_codigo, 1)
        Catch ex As Exception
            Throw New Exception("Erro ao localizar lista de itens")
        End Try
        Return tab
    End Function
#End Region
#Region "Localiza Cod"
    Public Function localizacod(ByVal tipo As Integer) As DataTable
        Try
            objDAL = New ContasPagarDAL
            Return objDAL.localizacod(tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    'localizaicp
    Public Function localizaicp(ByVal tipo As Integer) As DataTable
        Try
            objDAL = New ContasPagarDAL
            Return objDAL.localizaicp(tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region
#Region "Inserir"
    Function Inserir() As Integer
        Try
            If (validacampos()) Then
                objDAL = New ContasPagarDAL
                'aqui validar se o ipc_CODIGO >0 ALTERA SENAO ELE INSERE
                Me.ctp_codigo = objDAL.InserirAlterar(Me.ctp_codigo, Me.ccc_codigo, Me.com_codigo, Me.ctp_dtcompra, Me.ctp_descricao, Me.ctp_fornprestador, 1)
                If (Me.ctp_codigo > 0) Then
                    'excluir
                    insereItensCtasPagar()
                End If
            Else
                Throw New Exception("Preencher campos obrigatórios!")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return Me.ctp_codigo
    End Function
#End Region
#Region "Valida Campos"
    Public Function validacampos() As Boolean
        Try
            If (Me.ctp_fornprestador <> "") Then
                Return True
            End If
        Catch ex As Exception
        End Try
        Return False
    End Function
#End Region
#Region "Métodos Itens Contas Pagar (lista)"
    Sub insereItensCtasPagar()
        Try
            For Each item As ItensCtasPagarBLL In lCtasPagar
                item.ctpcodigo = Me.ctp_codigo
                item.InserirItensCtasPagar()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
   
#End Region
#Region "Desabilitados"
    'Function retccusto(ByVal forn As String) As DataTable 'retorna o centro custo
    '    Dim ret As DataTable
    '    Try
    '        objDAL = New ContasPagarDAL
    '        ret = objDAL.retccusto(forn)
    '    Catch ex As Exception
    '        Throw ex
    '    Finally
    '        objDAL = Nothing
    '    End Try
    '    Return ret
    'End Function
#End Region
End Class
